/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
 * 
 * Use is subject to license terms.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also
 * obtain a copy of the License at http://odftoolkit.org/docs/license.txt
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ************************************************************************/

using System.Xml.Linq;
using AODL.Document.Import.OpenDocument.NodeProcessors;
using AODL.Document.Styles;

namespace AODL.Document.Content.Text.Indexes
{
    /// <summary>
    /// Represent a Bookmark.
    /// </summary>
    public class Bookmark : IText, ICloneable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Bookmark"/> class.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <param name="type">The type.</param>
        /// <param name="bookmarkname">The bookmarkname.</param>
        public Bookmark(IDocument document, BookmarkType type, string bookmarkname)
        {
            Document = document;
            NewXmlNode(type, bookmarkname);
        }

        /// <summary>
        /// Gets or sets the name of the bookmark.
        /// </summary>
        /// <value>The name of the bookmark.</value>
        public string BookmarkName
        {
            get { return (string) Node.Attribute(Ns.Text + "name"); }
            set { Node.SetAttributeValue(Ns.Text + "name", value); }
        }

        /// <summary>
        /// Gets the type of the bookmark.
        /// </summary>
        /// <value>The type of the bookmark.</value>
        public BookmarkType BookmarkType
        {
            get
            {
                if (Node.Name == Ns.Text + "bookmark-start")
                    return BookmarkType.Start;
                if (Node.Name == Ns.Text + "bookmark-end")
                    return BookmarkType.End;
                return BookmarkType.Standard;
            }
        }

        /// <summary>
        /// Create the XElement.
        /// </summary>
        /// <param name="type">The bookmark type.</param>
        /// <param name="bookmarkname">The bookmark name.</param>
        private void NewXmlNode(BookmarkType type, string bookmarkname)
        {
            if (type == BookmarkType.Start)
                Node = new XElement(Ns.Text + "bookmark-start");
            else if (type == BookmarkType.End)
                Node = new XElement(Ns.Text + "bookmark-end");
            else
                Node = new XElement(Ns.Text + "bookmark");

            Node.SetAttributeValue(Ns.Text + "name", bookmarkname);
        }

        #region IText Member

        /// <summary>
        /// The node that represent the text content.
        /// </summary>
        /// <value></value>
        public XElement Node { get; set; }

        /// <summary>
        /// Gets or sets the node.
        /// </summary>
        /// <value>The node.</value>
        XNode IContent.Node
        {
            get { return Node; }
            set { Node = (XElement) value; }
        }

        /// <summary>
        /// The text.
        /// </summary>
        /// <value></value>
        public string Text
        {
            get { return BookmarkName; }
            set { BookmarkName = value; }
        }

        /// <summary>
        /// The document to which this text content belongs to.
        /// </summary>
        /// <value></value>
        public IDocument Document { get; set; }

        /// <summary>
        /// Is null no style is available.
        /// </summary>
        /// <value></value>
        public IStyle Style
        {
            get { return null; }
            set { }
        }

        /// <summary>
        /// No style name available
        /// </summary>
        /// <value></value>
        public string StyleName
        {
            get { return null; }
            set { }
        }

        #endregion

        #region ICloneable Member

        /// <summary>
        /// Create a deep clone of this Bookmark object.
        /// </summary>
        /// <remarks>A possible Attached Style wouldn't be cloned!</remarks>
        /// <returns>
        /// A clone of this object.
        /// </returns>
        public object Clone()
        {
            Bookmark bookmarkClone = null;

            if (Document != null && Node != null)
            {
                TextContentProcessor tcp = new TextContentProcessor();
                bookmarkClone = tcp.CreateBookmark(
                    Document, new XElement(Node), BookmarkType);
            }

            return bookmarkClone;
        }

        #endregion
    }

    /// <summary>
    /// Enum that represent the possible Bookmark types.
    /// </summary>
    public enum BookmarkType
    {
        /// <summary>
        /// A StartBookmark, requires also a EndBookmark!
        /// </summary>
        Start,
        /// <summary>
        /// Standard bookmark
        /// </summary>
        Standard,
        /// <summary>
        /// A EndBookmark
        /// </summary>
        End
    }
}

/*
 * $Log: Bookmark.cs,v $
 * Revision 1.2  2008/04/29 15:39:47  mt
 * new copyright header
 *
 * Revision 1.1  2007/02/25 08:58:40  larsbehr
 * initial checkin, import from Sourceforge.net to OpenOffice.org
 *
 * Revision 1.2  2006/02/02 21:55:59  larsbm
 * - Added Clone object support for many AODL object types
 * - New Importer implementation PlainTextImporter and CsvImporter
 * - New tests
 *
 * Revision 1.1  2006/01/29 11:28:22  larsbm
 * - Changes for the new version. 1.2. see next changelog for details
 *
 * Revision 1.2  2005/12/12 19:39:17  larsbm
 * - Added Paragraph Header
 * - Added Table Row Header
 * - Fixed some bugs
 * - better whitespace handling
 * - Implmemenation of HTML Exporter
 *
 * Revision 1.1  2005/11/20 17:31:20  larsbm
 * - added suport for XLinks, TabStopStyles
 * - First experimental of loading dcuments
 * - load and save via importer and exporter interfaces
 *
 */